﻿// <copyright file="OclSplit.cs" company="Codeplex project Eco.Contrib"> 
// The MIT License (MIT) 
// </copyright> 
// <author>Peter Buchmann</author> 

namespace Eco.Contrib
{
    using Eco.ObjectRepresentation;
    using Eco.Ocl.Support;

    /// <summary>
    /// OCL function split
    /// </summary>
    [EcoOclOperation(typeof(OclSplit), true)]
    public class OclSplit : OclOperationBase
    {
        /// <summary>
        /// Implementation of OCL funktion split
        /// </summary>
        /// <param name="oclParameters">The parameters.</param>
        public override void Evaluate(IOclOperationParameters oclParameters)
        {
            if (oclParameters == null)
            {
                throw new EcoContribException("Parameter is null");
            }

            string stringValue = Support.GetAsString(oclParameters.Values[0]);
            string charValue = Support.GetAsString(oclParameters.Values[1]);
            string[] stringList = stringValue.Split(charValue.ToCharArray());

            oclParameters.Result.SetDefaultValue(Support);
            if (string.IsNullOrEmpty(stringValue))
            {
                foreach (string stringLoop in stringList)
                {
                    IElement ie = Support.CreateNewConstant(Support.StringType, stringLoop);
                    oclParameters.Result.Element.GetAsCollection().Add(ie);
                }
            }
        }

        /// <summary>
        /// Initialisation of OCL function split
        /// </summary>
        protected override void Init()
        {
            IOclType[] parameter = new IOclType[2];
            parameter[0] = Support.StringType;
            parameter[1] = Support.StringType;
            InternalInit("split", parameter, Support.StringListType);
        }
    }
}
